from message.function.word_detect import *
import pickle
from message.function.Conversation import *

who_is_lsp = {}
configuration = json.load(open("F:\\qq机器人\\QQChatbotRelease\\QQChatbotRelease\\config.json", encoding='utf-8'))
detect_statement = configuration["detect_statement"]
bot_set = configuration["bot_set"]
music_set = configuration["music_set"]
detect_func = configuration["detect_func"]
#print(detect_func)

try:
    with open('./data/user_info/who_is_lsp', 'rb') as f:
        who_is_lsp = pickle.load(f)
except:
    pass


def judge_res(msg, group_or_not, user_id):
    print("using judge_res, msg is ",msg)
    # --------------------------------------------------------------------------------------发送涩图
    if group_or_not:
        if "ghs_pic" in detect_func:
            # 发送不特别瑟瑟的
            res_flg = ghs_pic(msg)
            if res_flg[0]:
                try:
                    count = who_is_lsp[user_id]
                    who_is_lsp[user_id] = int(count) + 1
                except:
                    who_is_lsp[user_id] = 1
                with open('./data/user_info/who_is_lsp', 'wb') as f:
                    pickle.dump(who_is_lsp, f)
                return res_flg[1]
    else:
        if "hs_pic" in detect_func:
            # 发送瑟瑟的
            res_flg = hs_pic(msg)
            if res_flg[0]:
                return res_flg[1]
            # --------------------------------------------------------------------------------------教说话
            '''
            res_flg = add_data(msg)
            if res_flg[0]:
                return res_flg[1]
            res_flg = del_data(msg)
            if res_flg[0]:
                return res_flg[1]
            '''
    # --------------------------------------------------------------------------------------谁是lsp
    res_flg = get_lsp(msg, who_is_lsp)
    print(res_flg)
    if res_flg[0]:
        return res_flg[1]

    res_flg = del_data(msg)
    print(res_flg)
    if res_flg[0]:
        return res_flg[1]

    res_flg = help_menu(msg)
    print(res_flg)
    if res_flg[0]:
        return res_flg[1]
        
    # 用循环代替
    #print(detect_func)
    for func in detect_func:
        if func == 'get_fortune':
            print("开始调用功能函数：", func)
            res_flg = eval(func)(msg,user_id)
            print(res_flg)
            if res_flg[0] or res_flg[0] == 'None':
                return res_flg[1]
        else:
            print("开始调用功能函数：",func)
            res_flg = eval(func)(msg)
            print(res_flg)
            if res_flg[0] or res_flg[0]=='None':
                return res_flg[1]
    return False
